package com.zzyl.nursing.service.impl;



import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingProjectPlanDto;
import com.zzyl.nursing.service.INursingProjectPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author syl
 * @date 2025-05-23
 */
@Service
public class NursingPlanServiceImpl  extends ServiceImpl<NursingPlanMapper, NursingPlan>  implements INursingPlanService {

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int  saveWithProject(NursingPlanDto dto){

        try {
            NursingPlan nursingPlan = new NursingPlan();

            // 先保存护理计划
            BeanUtils.copyProperties(dto,nursingPlan);  // 拷贝属性和类型要一致

            this.save(nursingPlan); // MP会自动返回主键值
            // 再保存中间表

            List<NursingProjectPlanDto> projectPlanDtos = dto.getProjectPlans();

            List<NursingProjectPlan> projectPlans = projectPlanDtos.stream().map(dto2->{
                NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
                nursingProjectPlan.setPlanId(nursingPlan.getId());
                nursingProjectPlan.setProjectId(Long.parseLong(dto2.getProjectId()));
                nursingProjectPlan.setExecuteTime(dto2.getExecuteTime());
                nursingProjectPlan.setExecuteCycle(dto2.getExecuteCycle().intValue());
                nursingProjectPlan.setExecuteFrequency(dto2.getExecuteFrequency().intValue());
                return nursingProjectPlan;
            }).collect(Collectors.toList());

            nursingProjectPlanService.saveBatch(projectPlans);
        } catch (BeansException e) {
            e.printStackTrace();
            return 0;
        }


        return 1;
    }

    @Override
    public NursingPlanVo getVoById(Integer id) {

        // 1、查询护理计划
        NursingPlan nursingPlan = getById(id);
        NursingPlanVo vo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,vo);
        // 2、根据护理计划的id查询中间表
        LambdaQueryWrapper<NursingProjectPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingProjectPlan::getPlanId,id);
        List<NursingProjectPlan> projectPlans = nursingProjectPlanService.list(queryWrapper);
        List<NursingProjectPlanVo> nursingProjectPlanVos = projectPlans.stream().map(projectPlan -> {
            NursingProjectPlanVo npvo = new NursingProjectPlanVo();
            npvo.setProjectId(projectPlan.getProjectId());
            npvo.setId(projectPlan.getId().toString());
            npvo.setPlanId(projectPlan.getPlanId().toString());
            npvo.setExecuteTime(projectPlan.getExecuteTime());
            npvo.setExecuteCycle(projectPlan.getExecuteCycle().toString());
            npvo.setExecuteFrequency(projectPlan.getExecuteFrequency().toString());

            return npvo;
        }).collect(Collectors.toList());


        vo.setProjectPlans(nursingProjectPlanVos);

        return vo;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateWithProjectById(NursingPlanDto dto) {

        try {
            NursingPlan nursingPlan = new NursingPlan();

            // 更新护理计划
            BeanUtils.copyProperties(dto,nursingPlan);  // 拷贝属性和类型要一致

            this.updateById(nursingPlan);

            // 先根据护理计划删除中间表的数据
            LambdaQueryWrapper<NursingProjectPlan> queryWrapper  = new LambdaQueryWrapper<>();
            queryWrapper.eq(NursingProjectPlan::getPlanId,nursingPlan.getId());
            nursingProjectPlanService.remove(queryWrapper);


            // 再新增中间表的数据

            List<NursingProjectPlanDto> projectPlanDtos = dto.getProjectPlans();

            List<NursingProjectPlan> projectPlans = projectPlanDtos.stream().map(dto2->{
                NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
                nursingProjectPlan.setPlanId(nursingPlan.getId());
                nursingProjectPlan.setProjectId(Long.parseLong(dto2.getProjectId()));
                nursingProjectPlan.setExecuteTime(dto2.getExecuteTime());
                nursingProjectPlan.setExecuteCycle(dto2.getExecuteCycle().intValue());
                nursingProjectPlan.setExecuteFrequency(dto2.getExecuteFrequency().intValue());
                return nursingProjectPlan;
            }).collect(Collectors.toList());

            nursingProjectPlanService.saveBatch(projectPlans);
        } catch (BeansException e) {
            e.printStackTrace();
            return 0;
        }


        return 1;

    }

    @Override
    public int removeWithProjectById(Integer planId) {
        try {
            // 删除护理计划
            removeById(planId);

            // 根据planId再删除中间表
            LambdaQueryWrapper<NursingProjectPlan> queryWrapper  = new LambdaQueryWrapper<>();
            queryWrapper.eq(NursingProjectPlan::getPlanId,planId);
            nursingProjectPlanService.remove(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
}
